package com.example.springcloud.controller;


import com.example.springcloud.entity.CommonResult;
import com.example.springcloud.entity.Payment;
import com.example.springcloud.serivce.PaymentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author chensixiang chensixiang1234@gmail.com
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/payment")
public class PaymentController {
    private final PaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/add")
    public CommonResult<Object> add(@RequestBody Payment payment) {
        int res = paymentService.create(payment);
        log.info("********插入结果: {}", res);
        if (res > 0) {
            return new CommonResult<>(200, "插入成功serverPort: " + serverPort, res);
        } else {
            return new CommonResult<>(444, "插入失败serverPort: " + serverPort, null);
        }
    }

    @GetMapping("/get/{id}")
    public CommonResult<Payment> get(@PathVariable Long id) {
        Payment payment = paymentService.findById(id);
        log.info("********插入结果: {}", payment);
        System.out.println("good");
        if (payment != null) {
            return new CommonResult<>(200, "插入成功serverPort: " + serverPort, payment);
        } else {
            return new CommonResult<>(444, "插入失败serverPort: " + serverPort, null);
        }
    }

    @GetMapping("/feign/timeout")
    public String timeout() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }

    @GetMapping("/lb")
    public String lb() {
        return serverPort;
    }

}
